<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Tymon\JWTAuth\Exceptions\JWTException;

class LoginController extends Controller
{

    public function login(Request $request)
    {
        $username = $request->get('username');
        $password = $request->get('password');
        $user = Admin::whereUsername($username)->first();
        if (!$user) {
            return showMsg('账号不存在', 201);
        }
        if (!password_verify($password, $user->password)) {
            return showMsg('密码输入不正确', 201);
        }
        try {
            $token = Auth::guard('admin')->attempt(['username' => $username, 'password' => $password]);
        } catch (JWTException $e) {
            return showMsg('生成token失败' . $e->getMessage());
        }
        return showMsg('登陆成功', 200, $token);
    }

}
